package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.BetterDrawPileToHandAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.ThornsPower;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.actions.SelectCardInCardGroupAction;
import demoMod.invincibleOne.enums.AbstractCardEnum;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.IOnCurseAddToHand;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class Seek extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("Seek");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Seek.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.NONE;

    private static final int COST = -2;

    public Seek() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 1;
        this.tags.add(CardTagsEnum.VIRUS);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            upgradeMagicNumber(1);
        };
    }
    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new SelectCardInCardGroupAction(this.magicNumber, c -> true, c -> {
            if (p.hand.size() >= BaseMod.MAX_HAND_SIZE) {
                p.createHandIsFullDialog();
            } else {
                returnToHand(c);
                if (c.hasTag(CardTagsEnum.VIRUS)) {
                    c.use(p, m);
                    IOnCurseAddToHand.curseAddToHand(c);
                } else if (c.type == CardType.CURSE) {
                    IOnCurseAddToHand.curseAddToHand(c);
                }
            }
        }, p.drawPile, SelectCardInCardGroupAction.TEXT[2]));
    }

    private void returnToHand(AbstractCard card) {
        card.unhover();
        card.lighten(true);
        card.setAngle(0.0F);
        card.drawScale = 0.12F;
        card.targetDrawScale = 0.75F;
        if (AbstractDungeon.player.drawPile.contains(card)) {
            card.current_x = CardGroup.DRAW_PILE_X;
        } else {
            card.current_x = CardGroup.DISCARD_PILE_X;
        }
        AbstractPlayer p = AbstractDungeon.player;
        card.current_y = CardGroup.DRAW_PILE_Y;
        p.drawPile.removeCard(card);
        p.hand.addToTop(card);
        p.hand.refreshHandLayout();
        p.hand.applyPowers();
    }
}
